package com.yylc.cscore.controller;

import cn.hutool.core.util.StrUtil;
import com.yylc.cscore.bean.TaskBean;
import com.yylc.cscore.bean.query.ServiceTaskQueryBean;
import com.yylc.cscore.entity.CustomerEntity;
import com.yylc.cscore.service.TaskService;
import com.yylc.cscore.util.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author xiangnan
 * date 2018/5/8
 */
@RequestMapping("/task")
@RestController
public class TaskController {

    @Resource
    private TaskService taskService;

    /**
     * 根据查询条件查询已分配的任务列表
     */
    @PostMapping("/page/list/query")
    public Object serviceTaskQuery(ServiceTaskQueryBean.Raw rawBean, HttpSession session) {
        // 内部会进行参数检查
        ServiceTaskQueryBean queryBean = rawBean.convert();

        Object cache = session.getAttribute("user");
        if (cache == null) {
            throw new RuntimeException("未登录");
        }

        // 非管理员写入serviceId
        CustomerEntity user = (CustomerEntity) cache;
        if (!user.isAdmin()) {
            queryBean.setServiceId(user.getServiceId());
        }

        return taskService.serviceTaskQuery(queryBean);
    }

    @PostMapping("/user/id")
    public Object serviceTaskQuery(@RequestParam String userId) {
        if (StrUtil.isBlank(userId)) {
            throw new RuntimeException("参数错误");
        }

        return taskService.serviceTaskQueryByUserId(userId);
    }

    @PostMapping("/update/user/task/list")
    public Object updateUserTaskList(List<TaskBean> taskList) {
        System.out.println(taskList);
        return 0;
    }

    @PostMapping("/update/user/task")
    public Object updateUserTask(TaskBean task) {
        System.out.println(task);
        return 0;
    }

    @PostMapping("/page/list/taskcode/{allotStatus}")
    public Object getTaskPageList(@PathVariable int allotStatus, @RequestParam String taskCode,
                                  @RequestParam String userType,
                                  @RequestParam int current, @RequestParam int size) {
        if (allotStatus < R.allotStatus.WAIT  || allotStatus > R.allotStatus.FINISH) {
            throw new RuntimeException("参数错误");
        }
        if (!R.userType.YYX.equals(userType) && !R.userType.YYLC.equals(userType)) {
            throw new RuntimeException("参数错误");
        }
        if (current < 1 || size < 1) {
            throw new RuntimeException("参数错误");
        }

        return taskService.getWaitAllotList(allotStatus, userType, StrUtil.isBlank(taskCode) ? null : taskCode,
                current, size);
    }

    @PutMapping("/{time}")
    public Object updateTask(TaskBean taskBean, @PathVariable String time) {
        if (taskBean == null || taskBean.getId() <= 0 || Long.parseLong(time) < 0) {
            throw new RuntimeException("参数错误");
        }

        taskBean.setOperateTime(Long.valueOf(time) > 0 ? new Date(Long.valueOf(time)) : null);
        return taskService.updateTask(taskBean);
    }

    @GetMapping("/label/list")
    public Object getTaskLabelList(@RequestParam String taskCode, @RequestParam String labelName) {
        return taskService.getTaskLabelList(StrUtil.isBlank(taskCode) ? null : taskCode,
                StrUtil.isBlank(labelName) ? null : labelName);
    }

    // ----------------------------------------------
    // 用户标定提供接口
    // ----------------------------------------------

    @GetMapping("/user/label/list")
    public Object getUserLabelList() {
        return taskService.getUserLabelList();
    }

    @GetMapping("/usr/label/{userId}")
    public Object getUserLabel(@PathVariable String userId) {
        if (StrUtil.isBlank(userId)) {
            throw new RuntimeException("参数错误");
        }

        return taskService.getUserLabel(userId);
    }

    @PutMapping("/usr/label/{userId}")
    public Object updateUserLabel(@PathVariable String userId, @RequestParam String label) {
        if (StrUtil.isBlank(userId)) {
            throw new RuntimeException("参数错误");
        }

        return taskService.updateUserLabel(label, userId);
    }

    // ----------------------------------------------
    // 业务类型提供接口
    // ----------------------------------------------

    @GetMapping("/business/type/{parentId}")
    public Object getBusinessTypeList(@PathVariable String parentId) {
        if (StrUtil.isBlank(parentId) || Integer.parseInt(parentId) < 0) {
            throw new RuntimeException("参数错误");
        }

        return taskService.getCategoryList(parentId);
    }

    // ----------------------------------------------
    // 客服申请任务回收接口
    // ----------------------------------------------

    @PutMapping("/recycle/customer/{customerId}")
    public Object recycleCustomerTask(@PathVariable String customerId) throws Exception {
        if (StrUtil.isBlank(customerId)) {
            throw new RuntimeException("参数错误");
        }

        return taskService.recycleCustomerTask(customerId);
    }

    // ----------------------------------------------
    // 注册渠道、下载渠道
    // ----------------------------------------------

    @GetMapping("/channel/list/{serviceId}")
    public Object getChannelList(@PathVariable String serviceId) {
        return taskService.getChannelList(serviceId);
    }

}
